package com.eke.action.comment;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.eke.bean.Article;
import com.eke.bean.Comment;
import com.eke.bean.Users;
import com.eke.service.ArticleService;
import com.eke.service.CommentService;
import com.eke.service.UsersService;
import com.jason.mvc.annotations.Ajax;
import com.jason.mvc.annotations.Namespace;
import com.jason.mvc.annotations.RequestMapping;
import com.jason.mvc.utils.ApplicationContext;

@Namespace("/comment")
@Component("commentAction")
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class CommentAction {
	@Resource
	private CommentService commentService;
	@Resource
	private UsersService usersService;
	@Resource
	private ArticleService articleService;
	private Comment comment;
	private Article article;
	private Integer pageCount = 1;

	private Integer pageSize = 7;

	/**
	 * 添加文章评论
	 * 
	 * @return Boolean
	 */
	@RequestMapping("addArticleComment")

	@Ajax(true)
	public boolean addArticleComment() {
		// 获取全局变量
		javax.servlet.http.HttpSession session = ApplicationContext.getSession();
		if (session.getAttribute("users.userId") != null) {
			Integer userId = (Integer) session.getAttribute("users.userId");
			comment.setUserId(userId);
		}
		articleService.addCommentNumber(article.getArticleId());
		comment.setCommentTime(new Timestamp(new Date().getTime()));
		comment.setArticleId(article.getArticleId());
		return commentService.addArticleComment(comment);
	}

	/**
	 * 查询所有文章评论
	 * 
	 * @return List
	 */
	@RequestMapping("queryArticleComment")
	@Ajax(true)
	public Map<String, Object> queryArticleComment() {

		Map<String, Object> map = new HashMap<String, Object>();
		List<Users> listUser = new ArrayList<Users>();
		int totalRows = commentService.queryArticleCommentTotal(article.getArticleId());
		int totalPage = getTotalPage(totalRows);
		List<Comment> list = commentService.queryArticleComment(article.getArticleId(),
				(getPageCount() - 1) * getPageSize(), getPageSize());
		for (Comment comment : list) {
			Users commentUser = usersService.queryUserDetails(comment.getUserId());
			commentUser.setUserPwd("");
			commentUser.setUserPhone("");
			commentUser.setUserEmail("");
			commentUser.setCreateTime(null);
			commentUser.setCourseId(null);
			commentUser.setLastLoginTime(null);
			listUser.add(commentUser);
		}

		map.put("totalPage", totalPage);
		map.put("pageCount", getPageCount());
		map.put("pageSize", getPageSize());
		map.put("prePage", getPageCount() - 1);
		int nextPage = getPageCount() + 1;
		map.put("nextPage", totalPage < nextPage ? 0 : nextPage);
		map.put("usersRow", listUser);
		map.put("row", list);
		return map;
	}

	/**
	 * 查询评论回复
	 * 
	 * @return List
	 */
	@RequestMapping("queryCommentRq")
	@Ajax(true)
	public Map<String, Object> queryCommentRq() {

		Map<String, Object> map = new HashMap<String, Object>();
		List<Users> listUser = new ArrayList<Users>();
		int totalRows = commentService.queryCommentRqTotal(comment.getCommentId());
		int totalPage = getTotalPage(totalRows);
		List<Comment> list = commentService.qurryCommentRq(comment.getCommentId(), (getPageCount() - 1) * getPageSize(),
				getPageSize());
		for (Comment listcomment : list) {
			Users commentUser = usersService.queryUserDetails(listcomment.getUserId());
			commentUser.setUserPwd("");
			commentUser.setUserPhone("");
			commentUser.setUserEmail("");
			commentUser.setCreateTime(null);
			commentUser.setCourseId(null);
			commentUser.setLastLoginTime(null);
			listUser.add(commentUser);
		}

		map.put("totalPage", totalPage);
		map.put("pageCount", getPageCount());
		map.put("pageSize", getPageSize());
		map.put("prePage", getPageCount() - 1);
		int nextPage = getPageCount() + 1;
		map.put("nextPage", totalPage < nextPage ? 0 : nextPage);
		map.put("usersRow", listUser);
		map.put("row", list);
		return map;
	}

	/**
	 * 添加评论回复
	 * 
	 * @return Boolean
	 */
	@RequestMapping("addCommentRq")
	@Ajax(true)
	public boolean addCommentRq() {
		// 获取全局变量
		javax.servlet.http.HttpSession session = ApplicationContext.getSession();
		if (session.getAttribute("users.userId") != null) {
			Integer userId = (Integer) session.getAttribute("users.userId");
			comment.setUserId(userId);
		}
		comment.setCommentTime(new Timestamp(new Date().getTime()));
		return commentService.addCommentRq(comment);
	}
	/**
	 * 查询页数
	 * @param totalRows
	 * @return
	 */
	public int getTotalPage(int totalRows) {
		int totalPage = totalRows / getPageSize();
		if (totalRows % getPageSize() > 0) {
			totalPage++;
		}
		return totalPage;
	}
	/**
	 * 查询第几页
	 * @param totalRows
	 * @return
	 */
	public Integer getPageCount() {
		return pageCount < 1 ? 1 : pageCount;
	}
	/**
	 * 查询每页数量
	 * @param totalRows
	 * @return
	 */
	public Integer getPageSize() {
		return pageSize < 0 ? 10 : pageSize;
	}

	/**
	 * 采纳评论
	 * 
	 * @return
	 */
	@RequestMapping("updateCommentState")
	@Ajax(true)
	public Map<String, Object> updateCommentState() {
		Map<String, Object> map = new HashMap<String, Object>();
		javax.servlet.http.HttpSession session = ApplicationContext.getSession();
		if (session.getAttribute("users.userId") != null) {
			Integer userId = (Integer) session.getAttribute("userId");
			if (userId != article.getUserId()) {
				map.put("result", "无权限");
			} else {
				if (article.getUserId() == comment.getUserId()) {
					map.put("result", "不能采纳自己的评论!!!");
				} else {
					map.put("result", commentService.updateCommentState(comment.getCommentId()));
				}
			}

		} else {
			map.put("result", "还未登陆!!");
		}
		return map;
	}
	
	/**
	 * 评论被采纳后，增加积分
	 * @param interal  积分数量
	 * @param commentUserId 评论用户id
	 * @return {@link Boolean} true 增加成功  false 增加失败
  	 */
	@RequestMapping("addInteral")
	@Ajax(true)
	public Map<String, Object> addInteral() { 
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", commentService.addInteral(article.getInteral(), comment.getUserId()));
		return map;
	}
	
}
